package cn.dengta.webapp.trade.dto.common;

import java.util.*;

import cn.dengta.context.model.WesKit;
import cn.dengta.webapp.trade.model.IntimacyLevel;
import cn.dengta.webapp.trade.model.IntimacyLevelAward;
import lombok.Getter;
import lombok.Setter;
import me.codeplayer.util.X;
import org.apache.commons.lang3.tuple.Pair;

@Getter
@Setter
public class IntimacyAwardConfig {

	private Map<WesKit, Map<IntimacyLevel, List<AwardConfigDTO>>> levelConfig;

	public Map<WesKit, Map<IntimacyLevel, List<AwardConfigDTO>>> getLevelConfig() {
		if (levelConfig == null) {
			levelConfig = new HashMap<>();
			for (WesKit kit : WesKit.values()) {
				levelConfig.put(kit, findOrDef(kit));
			}
		}
		return levelConfig;
	}

	public Map<IntimacyLevel, List<AwardConfigDTO>> findOrDef(WesKit wesKit) {
		if (X.isValid(this.levelConfig) && this.levelConfig.containsKey(wesKit)) {
			return this.levelConfig.get(wesKit);
		}
		Map<IntimacyLevel, List<AwardConfigDTO>> map = new HashMap<>(8, 1);
		map.put(IntimacyLevel.L1, List.of(new AwardConfigDTO().init(2, IntimacyLevelAward.A1)));
		if (wesKit == null) {
			map.put(IntimacyLevel.L2, List.of(
					//					new AwardConfigDTO().init(1, IntimacyLevelAward.A2),
					new AwardConfigDTO().init(null, IntimacyLevelAward.A6).buildIcon(wesKit)));
		} else {
			map.put(IntimacyLevel.L2, List.of(new AwardConfigDTO().init(1, IntimacyLevelAward.A2)));
		}
		map.put(IntimacyLevel.L3, List.of(new AwardConfigDTO().init(1, IntimacyLevelAward.A3).buildIcon(wesKit)));
		map.put(IntimacyLevel.L4, List.of(new AwardConfigDTO().init(5, IntimacyLevelAward.A4).buildIcon(wesKit)));
		map.put(IntimacyLevel.L5, List.of(new AwardConfigDTO().init(5, IntimacyLevelAward.A5).buildIcon(wesKit)));
		return map;
	}

	/**
	 * 判断小于等于当前等级的奖励中是否包含指定的奖励
	 */
	public Pair<IntimacyLevel, AwardConfigDTO> findLeLevelAndAwardAssign(WesKit kit, IntimacyLevel level, IntimacyLevelAward award) {
		if (kit == null || level == null || award == null) {
			return null;
		}
		Map<IntimacyLevel, List<AwardConfigDTO>> map = this.getLevelConfig().get(kit);
		for (Map.Entry<IntimacyLevel, List<AwardConfigDTO>> entry : map.entrySet()) {
			if (entry.getKey().gt(level)) {
				continue;
			}
			for (AwardConfigDTO dto : entry.getValue()) {
				if (dto.awards == award) {
					return Pair.of(entry.getKey(), dto);
				}
			}
		}
		return null;
	}

	public AwardConfigDTO findLeLevelAssign(WesKit kit, IntimacyLevel level, IntimacyLevelAward award) {
		Pair<IntimacyLevel, AwardConfigDTO> pair = findLeLevelAndAwardAssign(kit, level, award);
		return pair == null ? null : pair.getRight();
	}

	/** 奖励配置 */
	@Getter
	@Setter
	public static class AwardConfigDTO {

		/** 奖励枚举 */
		IntimacyLevelAward awards;
		/** 免费额度 */
		public Integer amount;
		/** 图片 */
		public String icon;

		public AwardConfigDTO init(Integer amount, IntimacyLevelAward awards) {
			this.awards = awards;
			this.amount = amount;
			return this;
		}

		public AwardConfigDTO buildIcon(WesKit kit) {
			Map<IntimacyLevelAward, String> map = IntimacyLevelAward.defIcon.get(kit);
			if (X.isValid(map)) {
				this.icon = map.get(this.awards);
			}
			return this;
		}

	}

}
